package vcp.client.GUI.mvc.view.addons;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.util.Rotation;

import vcp.client.statistics.PerformanceStatistics;

public class PerformancePieChartPanel extends JPanel
{

	/**
	 * 
	 */
	private static final long serialVersionUID = - 1950280874156013561L;

	private ChartPanel chartPanel;

	public PerformancePieChartPanel ( PerformanceStatistics statistics )
	{
		this.initChart(statistics);
		this.setupLayout();
	}

	private void setupLayout ()
	{
		this.setPreferredSize(new Dimension(908, 704));
		this.setLayout(new BorderLayout(0, 0));
		this.add(this.chartPanel);
	}

	private void initChart ( PerformanceStatistics statistics )
	{
		DefaultPieDataset dataSet = this.createCategoryDataSet(statistics);
		JFreeChart chart = this.createChart(dataSet, "Performance");
		this.chartPanel = new ChartPanel(chart);
	}

	private JFreeChart createChart ( DefaultPieDataset dataSet, String title )
	{
		JFreeChart chart = ChartFactory.createPieChart3D(title, dataSet, true, true, false);

		PiePlot3D plot = (PiePlot3D) chart.getPlot();
		plot.setStartAngle(290);
		plot.setDirection(Rotation.CLOCKWISE);
		plot.setForegroundAlpha(0.5f);
		return chart;
	}

	private DefaultPieDataset createCategoryDataSet ( PerformanceStatistics statistics )
	{
		DefaultPieDataset pieDataSet = new DefaultPieDataset();

		pieDataSet.setValue(statistics.getOrderAheadTitle(), statistics.getNumOrderAhead());
		pieDataSet.setValue(statistics.getCasualTitle(), statistics.getNumCasual());
		pieDataSet.setValue(statistics.getBusinessCasualTitle(), statistics.getNumBusinessCasual());
		pieDataSet.setValue(statistics.getFullTitle(), statistics.getNumFull());
		pieDataSet.setValue(statistics.getBusinessFullTitle(), statistics.getNumBusinessFull());

		return pieDataSet;
	}
}
